package com.ecfront.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.ecfront.dao.AdminDAO;
import com.ecfront.pojo.Admin;

/**
 * @version 1.0
 * @author www.ecfrom.com
 */

@SuppressWarnings("unchecked")
public class AdminDAOImpl extends HibernateDaoSupport implements AdminDAO {

	public static final String ADMIN_NAME = "adminName";

	protected void initDao() {}
	/**
	 * 增加管理员
	 * @param persistentInstance 需增加的管理员
	 */
	public void save(Admin transientInstance) {
		getHibernateTemplate().saveOrUpdate(transientInstance);
	}
	/**
	 * 删除管理员
	 * @param persistentInstance 需删除的管理员
	 */
	public void delete(Admin persistentInstance) {
		getHibernateTemplate().delete(persistentInstance);
	}
	/**
	 * 查找指定对象
	 * @param propertyName 查找的字段
	 * @param value 查找所对应的字段值
	 */
	public List findByProperty(String propertyName, Object value) {
		String queryString = "from Admin as model where model."
					+ propertyName + "= ?";
		return getHibernateTemplate().find(queryString, value);

	}
	/**
	 * 根据管理员名查找管理员
	 * @param adminName 需查找的管理员名
	 */
	public List findByAdminName(Object adminName) {
		return findByProperty(ADMIN_NAME, adminName);
	}
	/**
	 * 查找所有的管理员
	 */
	public List findAll() {
		String queryString = "from Admin";
		return getHibernateTemplate().find(queryString);
	}
	/**
	 * 根据ID返回Admin对象
	 * @param id 所需查找对象的ID
	 */
	public Admin findByAdminId(java.lang.Integer id) {
		Admin instance = (Admin) getHibernateTemplate().get(
					"com.ecfront.pojo.Admin", id);
		return instance;

	}
	/**
	 * 根据用户名和密码返回管理员
	 * @param adminName 所需用户名
	 * @param adminPassword 所需密码
	 */
	public Admin findAdminByAdminNameAndAdminPassword(String adminName,
			String adminPassword) {
		List ul = getHibernateTemplate()
				.find(
						"from Admin au where au.adminName = ? and au.adminPassword = ?",
						new String[] { adminName, adminPassword });
		if (ul != null && ul.size() >= 1) {
			return (Admin) ul.get(0);
		}
		return null;
	}

	public static AdminDAOImpl getFromApplicationContext(ApplicationContext ctx) {
		return (AdminDAOImpl) ctx.getBean("AdminDAO");
	}
}